package cn.edu.jnu.supershopper;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.Map;


public class BookListMainActivity extends AppCompatActivity {

public class PageViewFragmentAdapt extends FragmentStateAdapter {

    public PageViewFragmentAdapt(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch(position)
        {
            case 0:
                return BookFragment.newInstance();
            case 1:
                return BrowserFragment.newInstance();
            case 2:
                return MapFragment.newInstance();
            case 3:
                return GameFragment.newInstance();
        }
        return BookFragment.newInstance();
    }

    @Override
    public int getItemCount() {
        return 4;
    }
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list_main);
        ViewPager2 viewPager2Book = findViewById(R.id.viewpager2_main);
        viewPager2Book.setAdapter(new PageViewFragmentAdapt(getSupportFragmentManager(),getLifecycle()));

        TabLayout tabLayout=findViewById(R.id.tablayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayout, viewPager2Book, (tab, position) -> {
            switch(position)
            {
                case 0:
                    tab.setText("图书");
                    break;
                case 1:
                    tab.setText("新闻");
                    break;
                case 2:
                    tab.setText("卖家");
                    break;
                case 3:
                    tab.setText("游戏");
                    break;
            }
        });
        tabLayoutMediator.attach();

    }

}
